<template>
  <div class="app-container">
    <t-form
      :data="queryParams"
      ref="queryRef"
      v-show="showSearch"
      layout="inline"
      class="mb20"
    >
      <t-form-item label="用户名称" prop="userName">
        <t-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="手机号码" prop="phonenumber">
        <t-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery"
            >搜索

            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery"
            >重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          theme="primary"
          @click="openSelectUser"
          v-hasPermi="['system:role:add']"
          >添加用户
          <template #icon>
            <t-icon name="add" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['system:role:remove']"
          >批量取消授权
          <template #icon>
            <t-icon name="close-circle" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button theme="warning" @click="handleClose"
          >关闭
          <template #icon>
            <t-icon name="close" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      v-loading="loading"
      :data="userList"
      row-key="userId"
      :columns="columns"
      @select-change="handleSelectionChange"
    >
      <template #status="scope">
        <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
      <template #action="scope">
        <t-button
          variant="text"
          theme="primary"
          @click="cancelAuthUser(scope.row)"
          v-hasPermi="['system:role:remove']"
          >取消授权
          <template #icon>
            <t-icon name="close-circle" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <select-user
      ref="selectRef"
      :roleId="queryParams.roleId"
      @ok="handleQuery"
    />
  </div>
</template>

<script setup name="AuthUser">
import selectUser from "./selectUser";
import {
  allocatedUserList,
  authUserCancel,
  authUserCancelAll,
} from "@/api/system/role";

const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  userName: undefined,
  phonenumber: undefined,
});

const columns = ref([
  {
    colKey: "row-select",
    title: "选择",
    width: 55,
    align: "center",
    type: "multiple",
  },
  {
    colKey: "userName",
    title: "用户名称",
    width: 120,
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "nickName",
    title: "用户昵称",
    width: 120,
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "email",
    title: "邮箱",
    width: 120,
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "phonenumber",
    title: "手机",
    width: 120,
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "status",
    title: "状态",
    width: 80,
    align: "center",
  },
  {
    colKey: "createTime",
    title: "创建时间",
    width: 180,
    align: "center",
  },
  {
    colKey: "action",
    title: "操作",
    width: 120,
    align: "center",
    fixed: "right",
  },
]);

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  allocatedUserList(queryParams).then((response) => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/system/role" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  userIds.value = selectedRowData.map((item) => item.userId);
  multiple.value = !selectedRowKeys.length;
}
/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs["selectRef"].show();
}
/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  proxy.$modal
    .confirm('确认要取消该用户"' + row.userName + '"角色吗？')
    .then(function () {
      return authUserCancel({ userId: row.userId, roleId: queryParams.roleId });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
/** 批量取消授权按钮操作 */
function cancelAuthUserAll(row) {
  const roleId = queryParams.roleId;
  const uIds = userIds.value.join(",");
  proxy.$modal
    .confirm("是否取消选中用户授权数据项?")
    .then(function () {
      return authUserCancelAll({ roleId: roleId, userIds: uIds });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}

getList();
</script>
